const App = {
    // mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            Data: [],
            page:{},
            nodeKey:'',
            showDialog:true,
            form:{
                agencies:[],
            },
            organization:[],

        }
    },
    mounted() {
        let ls_URL = window.location.href;
        this.nodeKey = getQueryObject(ls_URL, "nodeKey");// 获取 nodeKey 参数
        this.nodeKey1 = getQueryObject(ls_URL, "nodeKey1");// 获取 nodeKey 参数
        this.handleShow( this.nodeKey);
    },
    methods: {
        handleShow(val) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: "t_innovation_mechanism_details_agencies",
                    Condition: '',
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND innovation_mechanism_details_agencies.Innovation_Mechanism_Detail_SerialNumber='" + val + "'"))),
                },
                success: (res) => {
                    // let arr = res.map(i => i.Government_Agencies_SerialNumber);
                    // let agencies = [];
                    // arr.forEach(ite => {
                    //     agencies.push(this.configValue(this.organization, ite));
                    // })
                    // this.form.agencies = agencies;
                    this.Data = res;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'government_agencies',
                    NodeCode: "",
                },
                success: (data) => {
                    this.organization = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        configValue(options, val) {
            for (let i = 0; i < options.length; i++) {
                const item = options[i];
                if (item.Government_Agencies_SerialNumber == val) {
                    return [item.Government_Agencies_SerialNumber];
                } else if (item.children && item.children.length > 0) {
                    let arr = this.configValue(item.children, val);
                    if (arr) {
                        return [item.Government_Agencies_SerialNumber, ...arr];
                    }
                }
            }
        },
        personalAdd(row) {
            window.location.href = './addzj.html?action=Add&nodeKey=' + this.nodeKey + '&nodeKey1=' +  this.nodeKey1 
        },
        xinwen() {
            window.location.href = '../../news_videos_files/news/add.html?action=Add&News_Category_SerialNumber=NC0000000000075'
        },
        handleEdit(row) {
            let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            window.location.href = "./addzj.html?action=Modify&nodeKey=" + row[primary_key];
        },
        //删除数据
        handleDelete(data) {
            this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let { TableName, primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: TableName,
                        Condition: primary_key + "='" + data[primary_key] + "'"
                    },
                    success: (res) => {
                        if (res.length == 1) {
                            res[0].Row_Status = '1';
                            let lJSONObject_DataSet = new Object();
                            let lJSONObject_Table = new Object();
                            lJSONObject_Table[TableName] = res;
                            lJSONObject_DataSet['Modify'] = lJSONObject_Table;
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            lJSONObject_DataSet['Add'] = new Object();
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSaveByRequestBody",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet, (res) => {
                                if (res.Status == 'success') {
                                    this.handleShow( this.nodeKey);
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: '删除失败'
                                    });
                                }
                            })
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败'
                            });
                        }

                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }).catch(() => { });
        },
        handleSubmit(){
            if (this.form.agencies.length > 0) {
                let str = this.form.agencies.map(i=>"'" + i + "'").join(',')
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: 'innovation_mechanism_details_agencies',
                        Condition: `Innovation_Mechanism_Detail_SerialNumber='${this.nodeKey}' AND Government_Agencies_SerialNumber IN (${str})`,
                    },
                    success: (data) => {
                        let newInnovation_mechanism_details_agencies = [];
                        this.form.agencies.forEach(item => {
                            let obj = data.filter(i=>i.Government_Agencies_SerialNumber == item[item.length - 1])[0];
                            if (obj) {
                                obj.Row_Status = '0'
                            } else {
                                newInnovation_mechanism_details_agencies.push({
                                    Innovation_Mechanism_Detail_SerialNumber:this.nodeKey,
                                    Government_Agencies_SerialNumber: item[item.length - 1]
                                })
                            }
                        });
                        let lJSONObject_DataSet = {
                            Add:{
                                Innovation_mechanism_details_agencies
                            },
                            Modify:{
                                Innovation_mechanism_details_agencies
                            }
                        }
                        lJSONObject_DataSet['Add']['base_operation_log'] = [{
                            Operation_Type: '0',
                            Operation_Path: this.relativePath,
                            Operation_Main,
                            Operation_Interface: "generalSaveByRequestBody",
                            Operation_IP: getIpAddress()
                        }]
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                this.handleShow( this.nodeKey);
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '添加失败'
                                });
                            }
                        })
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            } else {
                this.$message({
                    message: '请选择适用组织',
                    type: 'warning'
                });
            }
        },
        handleChange(val){
            if (this.Data.some(i => i.Government_Agencies_SerialNumber == val[val.length - 1][val[val.length - 1].length - 1] )) {
                this.form.agencies.pop();
                this.$message({
                    message: '该组织已存在',
                    type: 'warning'
                });
            }
        },
        goBack() {
            window.history.back();
        },
        reset(){
            window.location.reload();
        }
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
